package com.febers.ihealth.base

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import com.febers.ihealth.utils.log

abstract class BaseFragment: Fragment() {

    abstract fun setView(): Int

    abstract fun initView()

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        return inflater.inflate(setView(), container, false)
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        initView()
    }

    protected var lazyVisible = false

    override fun setUserVisibleHint(isVisibleToUser: Boolean) {
        super.setUserVisibleHint(isVisibleToUser)
        if (!lazyVisible && userVisibleHint) {
            log("执行延迟初始化")
            lazyVisible = true
            lazyInit()
        }
        if (userVisibleHint) {
            onVisible()
        }
    }

    abstract fun lazyInit()

    open fun onVisible() { }
}